/**
 * Author Highway
 */
package com.yuekao.servlet;

import com.yuekao.Service.Analyze;
import com.yuekao.mycrawlertest.db.mapper.SchoolMapper;
import com.yuekao.mycrawlertest.db.vo.School;
import com.yuekao.mycrawlertest.db.vo.pageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class common {

    @Autowired
    private SchoolMapper schoolMapper;

    /**
     *
     * @param request
     * @param id           获取当前页面的页数
     * @param schoolName 　获取当前学校名
     * @return
     */
    public  String show(HttpServletRequest request, Integer id, String schoolName) {
        //获取爬虫得到的内容信息
        String getHtmlContent = Analyze.contents;
        //获取爬虫得到的日期信息
        String getHtmlDate = Analyze.date;
        //传参数
        request.setAttribute("href", getHtmlContent);
        request.setAttribute("date", getHtmlDate);
        //获取数据中对应id的内容信息
        String getSqlContent = schoolMapper
                .selectByPrimaryKey(id)
                .getContents();
        //获取数据中对应id的时间信息
        String getSqlDate = schoolMapper.selectByPrimaryKey(id).getDate();
        //数据库内容与爬取内容进行比较,如果相同则不跳转，不同则会提示并且更新数据库
        if (getHtmlContent.equals(getSqlContent) && getHtmlDate.equals(getSqlDate)) {
            int totalNum = schoolMapper.getToTalNum();
            int pageNum = totalNum / 15;
            if ((totalNum % 15) != 0) {
                pageNum++;
            }
            List<pageBean> numbers = new ArrayList<>();
            for (int i = 1; i <= pageNum; i++) {
                pageBean p = new pageBean();
                p.setPageNum(i);
                numbers.add(p);

            }
            request.setAttribute("numbers", numbers);
            String page = request.getParameter("id");
            int pageCurNum = 1;
            if (page != null) {
                pageCurNum = Integer.valueOf(page);
            }



            List<School> list = schoolMapper.getInfo((pageCurNum - 1) * 15, 15);
            request.setAttribute("list", list);
            return "redirect:/index?pageNum="+(id/15+1);

        }
        //数据库更新
        else {
            School school = new School();
            school.setContents(getHtmlContent);
            school.setDate(getHtmlDate);
            school.setSchoolName(schoolName);
            school.setId(id);
            //System.out.println(getHtmlContent);
            schoolMapper.updateByPrimaryKey(school);
            //返回提示页面
            return "alert";
        }

    }
    //上海外国语大学单独提取工作

    /**
     * 基本内容同上
     * @param request
     * @param id
     * @param schoolName
     * @return
     */
    public  String showSisu(HttpServletRequest request, Integer id, String schoolName) {
        //获取目标内容
        String getHtmlContent = Analyze.sisuContents;
        //获取目标日期
        String getHtmlDate = Analyze.sisuDate;
        request.setAttribute("href", getHtmlContent);
        request.setAttribute("date", getHtmlDate);
        System.out.println(getHtmlContent);
        String getSqlContent = schoolMapper
                .selectByPrimaryKey(id)
                .getContents();
        String getSqlDate = schoolMapper.selectByPrimaryKey(id).getDate();
        System.out.println(id);
        System.out.println(schoolName);
        if (getHtmlContent.equals(getSqlContent) && getHtmlDate.equals(getSqlDate)) {
            int totalNum = schoolMapper.getToTalNum();
            System.out.println(totalNum);
            int pageNum = totalNum / 15;
            if ((totalNum % 15) != 0) {
                pageNum++;
            }
            List<pageBean> numbers = new ArrayList<>();
            for (int i = 1; i <= pageNum; i++) {
                pageBean p = new pageBean();
                p.setPageNum(i);
                numbers.add(p);

            }
            request.setAttribute("numbers", numbers);
            String page = request.getParameter("pageNum");
            int pageCurNum = 1;
            if (page != null) {
                pageCurNum = Integer.valueOf(page);
            }


            List<School> list = schoolMapper.getInfo((pageCurNum - 1) * 15, 15);
            request.setAttribute("list", list);
           // return "forward:/users/uc";
            return "\"redirect:/index?pageNum=\"+(id/15+1)";

        } else {
            School school = new School();
            school.setContents(getHtmlContent);
            school.setDate(getHtmlDate);
            school.setSchoolName(schoolName);
            school.setId(id);
            //System.out.println(getHtmlContent);
            schoolMapper.updateByPrimaryKey(school);
            return "alert";
        }

    }
}